package com.zhentao.payment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.payment.entity.RefundOrder;
import com.zhentao.payment.mapper.RefundOrderMapper;
import com.zhentao.payment.service.RefundService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * 退款服务实现类
 */
@Service
public class RefundServiceImpl extends ServiceImpl<RefundOrderMapper, RefundOrder> implements RefundService {
    /**
     * 创建退款申请
     * 1. 生成唯一的退款单号
     * 2. 设置退款初始状态为待退款
     * 3. 保存退款记录到数据库
     */
    @Override
    @Transactional
    public RefundOrder createRefund(String orderNo, Double amount, String reason) {
        RefundOrder refundOrder = new RefundOrder();
        refundOrder.setRefundNo("REF" + UUID.randomUUID().toString().replace("-", "").substring(0, 24));
        refundOrder.setOrderNo(orderNo);
        refundOrder.setAmount(amount);
        refundOrder.setReason(reason);
        refundOrder.setStatus(0); // 待处理
        refundOrder.setCreateTime(LocalDateTime.now());
        refundOrder.setUpdateTime(LocalDateTime.now());
        
        save(refundOrder);
        return refundOrder;
    }

    /**
     * 查询退款记录
     * 通过退款单号查询退款详细信息
     */
    @Override
    public RefundOrder queryRefund(String refundNo) {
        return getById(refundNo);
    }

    /**
     * 处理退款回调
     * 1. 查询退款记录
     * 2. 验证退款状态是否为待退款
     * 3. 根据退款结果更新退款状态
     * 4. 后续可以通过消息队列通知其他服务
     */
    @Override
    @Transactional
    public void approveRefund(String refundNo) {
        RefundOrder refundOrder = getById(refundNo);
        if (refundOrder == null) {
            throw new RuntimeException("退款申请不存在");
        }
        if (refundOrder.getStatus() != 0) {
            throw new RuntimeException("退款申请状态不正确");
        }
        
        refundOrder.setStatus(1); // 退款成功
        refundOrder.setUpdateTime(LocalDateTime.now());
        updateById(refundOrder);
    }

    @Override
    @Transactional
    public void rejectRefund(String refundNo) {
        RefundOrder refundOrder = getById(refundNo);
        if (refundOrder == null) {
            throw new RuntimeException("退款申请不存在");
        }
        if (refundOrder.getStatus() != 0) {
            throw new RuntimeException("退款申请状态不正确");
        }
        
        refundOrder.setStatus(2); // 退款失败
        refundOrder.setUpdateTime(LocalDateTime.now());
        updateById(refundOrder);
    }

    @Override
    public IPage<RefundOrder> queryRefundList(String refundNo, String orderNo, Integer status, Integer page, Integer size) {
        LambdaQueryWrapper<RefundOrder> wrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(refundNo)) {
            wrapper.eq(RefundOrder::getRefundNo, refundNo);
        }
        if (StringUtils.hasText(orderNo)) {
            wrapper.eq(RefundOrder::getOrderNo, orderNo);
        }
        if (status != null) {
            wrapper.eq(RefundOrder::getStatus, status);
        }
        
        wrapper.orderByDesc(RefundOrder::getCreateTime);
        
        return page(new Page<>(page, size), wrapper);
    }
}
